import { fetchAssetType } from '../services/api';

export default {
  namespace: 'assetTypeList',

  state: {
    list: [],
    loading: true, // request is fetching
    pagination: undefined,
  },

  effects: {
    *fetchList({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        paylaod: true,
      });
      const response = yield call(fetchAssetType, { payload });
      yield put({
        type: 'loadTypeList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
  },

  reducers: {
    changeLoading(state, { payload }) {
      return {
        ...state,
        loading: payload,
      };
    },
    loadTypeList(state, { payload }) {
      return {
        ...state,
        list: payload.type_list,
        totalPage: payload.total_page,
      };
    },
  },
};
